﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using System.Data.Services.Client;
using iTraffic_ClientWP.iTrafficService;
using System.Windows.Media.Imaging;
using Microsoft.Phone.Tasks;
using Microsoft.Xna.Framework.Media;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.Phone;
using System.Collections.Specialized;
using System.Net;
using System.IO;
using System.Device.Location;
using System.Text;
using System.Xml.Linq;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls.Maps.Core;
using iTraffic_ClientWP.Model;

namespace iTraffic_ClientWP
{
    public partial class MainPage : PhoneApplicationPage
    {
        private GeoCoordinateWatcher _watcher;

        CameraCaptureTask cameraCaptureTask;
        public HotPoints_Photos currentPhoto;
        public bool chooserCancelled;

        private byte[] _imageBytes;
        private string flag;
        string filename;
        string _filename;
        int _id;
        int userID;
        short? _like;
        short? _dislike;
        string Latitude;
        string Longitude;

        double? _latitude;
        double? _longitude;

        int levelId;
        int hotPointId;

        static string serviceUriString = UrlConfig.SERVICE_URI_STRING;

        // Declare the service root URI.
        private Uri svcRootUri =
            new Uri(serviceUriString, UriKind.Absolute);

        // Declare our private binding collection.
        private DataServiceCollection<HotPoints_Photos> _myphotos;
        private DataServiceCollection<HotPoints> _hotpoints;
        private DataServiceCollection<HotPoints_Levels> _level;

        private iTrafficEntities _context;

        public bool IsDataLoaded { get; private set; }
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            getHotPointList();
         //   getHotPointLevelList();
           

          

 
        }

        private void btn_Shot_Click(object sender, EventArgs e)
        {
            HotPoints_Photos newPhoto = HotPoints_Photos.CreateHotPoints_Photos(0, string.Empty, string.Empty,0,0,0);
            NavigationService.Navigate(new Uri("/Upload.xaml",UriKind.RelativeOrAbsolute));
            // Add the new photo to the tracking collection.
           // _myphotos.Add(newPhoto);
          
        }
        /*------------------------------UPLOAD - SAVE PHOTO--------------------------------*/
      
        /*---------------------------------------Save to Isolated---------------------------------------------*/
       



        /*---------------------------------------- Get Hot Point List and Hot Point Level-----------------------*/
        private void getHotPointList()
        {

            // Load data from the data service.
            _context = new iTrafficEntities(svcRootUri);

            // Call Add Header to Request
            // _context.SendingRequest += new EventHandler<SendingRequestEventArgs>(OnSendingRequest);

            _hotpoints = new DataServiceCollection<HotPoints>(_context);

            // Load the data from the Photo feed.
            _hotpoints.LoadAsync(new Uri(UrlConfig.GET_HOT_POINT_LIST, UriKind.Absolute));

            // Register for the LoadCompleted event to turn off the progress bar and load Thumbnail.
            _hotpoints.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(GetHotPointList_LoadCompleted);
        }

        private void GetHotPointList_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                // Make sure that we load all pages of the Photo feed.
                if (_hotpoints.Continuation != null)
                {
                    // Request the next page from the data service.
                    _hotpoints.LoadNextPartialSetAsync();
                }
                else
                {

                    List<HotPoint> _pointList = new List<HotPoint>();
                    for (int i = 0; i < _hotpoints.Count; i++)
                    {
                        HotPoints info = _hotpoints[i];
                        
                        _pointList.Add(new HotPoint(info.Id, info.Name,info.Longitude,info.Latitude));
                        Pushpin pin = new Pushpin();
                        pin.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(pin_MouseLeftButtonDown);
                        pin.Location = new GeoCoordinate(Double.Parse(info.Latitude), Double.Parse(info.Longitude));
                        pin.DataContext = info.Id;
                        pin.Template = (ControlTemplate)(this.Resources["PushpinControlTemplate1"]);
                        map1.Children.Add(pin);
                    }

                    //hotPoint_ListPicket.ItemsSource
                   // hotPoint_ListPicket.ItemsSource = _pointList;
                    
                  //  InitializePushpins();

                }

            }
            else
            {
                if (MessageBox.Show(e.Error.Message, "Retry request?",
                    MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {

                }
            }
        }

        void pin_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {

            Pushpin mapPushpin = sender as Microsoft.Phone.Controls.Maps.Pushpin;

            string pointId = mapPushpin.DataContext.ToString();
            var mouseMapPosition = e.GetPosition(map1);
            var point = new Point(mouseMapPosition.X, mouseMapPosition.Y);
            var location = map1.ViewportPointToLocation(point);

            NavigationService.Navigate(new Uri("/ViewPhotos.xaml?pointId=" + pointId, UriKind.RelativeOrAbsolute));
            //MessageBox.Show("id=" + a);
        }

        


        /*-------------------------- PUSH PIN----------------------------------*/
        private void InitializePushpins()
        {

            List<HotPoint> _levelList = new List<HotPoint>();
            for (int i = 0; i < _levelList.Count; i++)
            {
                Pushpin pin1 = new Pushpin();
                pin1.Location = new GeoCoordinate(Double.Parse(_levelList[i].lat), Double.Parse(_levelList[i].lang));
                pin1.DataContext = _levelList[i].id;
                pin1.Template = (ControlTemplate)(this.Resources["PushpinControlTemplate1"]);
                map1.Children.Add(pin1);
            }

         //   Pushpin pin1 = new Pushpin();
            
            //pin1.Location = new GeoCoordinate(21.0002702, 105.8159326);
            //pin1.Template = (ControlTemplate)(this.Resources["PushpinControlTemplate1"]);
            //map1.Children.Add(pin1);

            //Pushpin pin2 = new Pushpin();
            //pin2.Location = new GeoCoordinate(20.9988301, 105.8204573);
            //pin2.Template = (ControlTemplate)(this.Resources["PushpinControlTemplate1"]);
            //map1.Children.Add(pin2);

          //  map1.SetView(pin1.Location, 10);
        }

  
        private void Layer_1_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Pushpin mapPushpin = sender as Microsoft.Phone.Controls.Maps.Pushpin;

            string a = mapPushpin.DataContext.ToString();
            var mouseMapPosition = e.GetPosition(map1);
            var point = new Point(mouseMapPosition.X, mouseMapPosition.Y);
            var location = map1.ViewportPointToLocation(point);
          
           
            MessageBox.Show("id="+a);

        }
    }
}